package SynchronizedPackage;

import lombok.extern.slf4j.Slf4j;

public class TestMultiLock {
    public static void main(String[] args) {
        BigRoom bigRoom = new BigRoom();
        new Thread(() -> {
            try {
                bigRoom.study();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"小南").start();
        new Thread(() -> {
            try {
                bigRoom.sleep();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"小女").start();


        new Thread(() -> {
            try {
                bigRoom.studyMultiLock();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"小南2").start();
        new Thread(() -> {
            try {
                bigRoom.sleepMultiLock();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"小女2").start();
    }

}

@Slf4j
class BigRoom{
    private final Object studyRoom = new Object();
    private final Object bedRoom = new Object();

    public void sleep() throws InterruptedException {
        synchronized (this) {
            log.debug("sleeping 2小时");
            Thread.sleep(2000);
        }
    }

    public void study() throws InterruptedException {
        synchronized (this){
            log.debug("study 1小时");
            Thread.sleep(1000);
        }
    }

    public void sleepMultiLock() throws InterruptedException {
        synchronized (bedRoom) {
            log.debug("sleeping 2小时");
            Thread.sleep(2000);
        }
    }
    public void studyMultiLock() throws InterruptedException {
        synchronized (studyRoom) {
            log.debug("study 1小时");
            Thread.sleep(1000);
        }
    }


}
